package com.joshjung.hover.functional
{
    import com.joshjung.hover.interactive.projectiles.ProjectileBase;
    import com.joshjung.hover.interfaces.ILoopable;

    import flash.geom.Point;

    /**
     * Each weapon barrel points in a direction relative to its mounted object and fires a
     * certain type of projectile.
     */
    public class WeaponBarrel implements ILoopable
    {
        //---------------------------------------------------------------------
        //
        //  Variables
        //
        //---------------------------------------------------------------------
        /**
         * The parent weapon this is a part of.
         */
        protected var parent : Weapon;

        /**
         * The mount point relative to the mounted object (e.g. hovercraft)
         *
         * This is RELATIVE.
         */
        protected var mountPoint : Point;

        /**
         * The direction the gun is pointing, relative to the rotation of the mounted object.
         */
        protected var mountRotation : Number;

        /**
         * The projectile class... should implement IProjectile
         */
        protected var projectile : Class;

        /**
         * The number of projectiles to fire per second.
         */
        protected var rate : Number = 1;

        /**
         * The speed at which the projectile proceeds out of the gun barrel.
         */
        protected var speed : Number;

        protected var timeUntilNextFire : Number;

        protected var firing : Boolean = false;

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function WeaponBarrel(parent : Weapon, mountPoint : Point, mountRotation : Number, projectile : Class, rate : Number, speed : Number)
        {
            this.parent = parent;
            this.mountPoint = mountPoint;
            this.mountRotation = mountRotation;
            this.projectile = projectile;
            this.rate = rate;
            this.speed = speed;
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        public function loop(elapsed : Number) : void
        {
            if (firing)
            {
                timeUntilNextFire -= elapsed;

                if (timeUntilNextFire <= 0)
                {
                    var proj : ProjectileBase = new projectile(parent.parent.viewport, parent.parent.loopList);
                    proj.x = parent.parent.x + mountPoint.x;
                    proj.y = parent.parent.y + mountPoint.y;

                    var rot : Number = parent.parent.rotation + mountRotation;
                    var cos : Number = Math.cos(rot * 0.0174532925);
                    var sin : Number = Math.sin(rot * 0.0174532925);

                    proj.speed.x = (cos * speed) + parent.parent.speed.x;
                    proj.speed.y = (sin * speed) + parent.parent.speed.y;

                    parent.parent.loopList.insert(proj);

                    timeUntilNextFire = 1.0 / rate;
                }
            }
        }

        public function startFiring() : void
        {
            firing = true;
            timeUntilNextFire = 0;
        }

        public function stopFiring() : void
        {
            firing = false;
        }
    }
}
